package com.sdgakj.study.thread.pc.sychronized;

/**
 * @Author: Du Ronghong
 * @Description:
 * @Date: Created in 17:39 2018/8/28
 * @Modified By:
 */
public class Food {

    private int current;
    private int max = 10;

    public synchronized void remove() {
        if (current > 0) {
            System.out.println(Thread.currentThread().getName() + "消费者正在消费,产品数量:" + current);
            current--;
            notifyAll();
        } else {
            try {
                wait();
                System.out.println(Thread.currentThread().getName() + "停止消费,等待生产,剩余数量:" + current);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void add() {
        if (current < max) {
            current++;
            System.out.println(Thread.currentThread().getName() + "正在生产,产品数量:" + current);
            notifyAll();
        } else {
            try {
                wait();
                System.out.println(Thread.currentThread().getName() + "停止生产,等待消费,产品数量:" + current);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
